<?php

namespace Home\Controller;

use Think\Controller;

class LinkController extends Controller {
	/* Ajax通道 */
	function doAjax() {
		$type = $_GET ['type'];
		$data = $_GET ['data'];
		$returnMsg = "Init msg!";
		
		switch ($type) {
			case "getPublicLink" :
				$returnMsg = $this->getPublicLink ();
				break;
			case "getPrivateLink" :
				$returnMsg = $this->getPrivateLink ();
				break;
			case "delPrivateLink" :
				$returnMsg = $this->delPrivateLink ( $data );
				break;
			case "cenPublicLink" :
				$returnMsg = $this->cenPublicLink ( $data );
				break;
			case "getTypeLink" :
				$returnMsg = $this->getTypeLink ( $data );
				break;
			case "getType2Link" :
				$returnMsg = $this->getType2Link ( $data );
				break;
			case "toLinkLog" :
				$returnMsg = $this->toLinkLog ( $data );
				break;
			
			default :
				break;
		}
		
		$this->ajaxReturn ( $returnMsg );
	}
	/* 网站链接点击记录 */
	function toLinkLog($linkurl) {
		$logContent = I ( "session.nwb_userid", 10000 ) . "," . I ( "session.nwb_email", "visitor" ) . ",toLink," . $linkurl;
		writeLog ( $logContent, 'link' );
		return ture;
	}
	/* 网站一级分类获取 */
	function getTypeLink($type) {
		$redis = getObj ( 'redis' );
		$linkInfo = $redis->getType1Link ( $type );
		
		$table = "<table id='table_link' border='0' cellpadding='10' cellspacing='0'>" . C ( "html_web_type1_" . $type ) . "<tr>";
		$num = 0;
		foreach ( $linkInfo as $aLink ) {
			if ($aLink ['linkicon'] == 'publiceeb/guanggao.png')
				$aLink ['linkicon'] = 'publiceeb/guangg.jpg';
			
			if ($aLink ['linkicon'] == "")
				$aLink ['linkicon'] = "publiceeb/moren.png";
			
			$cenimgsrc = "Public/Image/cen1.png";
			if (in_array ( $aLink ['linkid'], session ( 'nwb_usercen' ) ))
				$cenimgsrc = "Public/Image/cen2.png";
			$num ++;
			$table .= "<td onmouseover=cenPublicLink('show','link_wrap_$aLink[linkid]') onmouseout=cenPublicLink('reset','link_wrap_$aLink[linkid]')><div class='link_wrap' id='link_wrap_$aLink[linkid]'>
				<a onclick=toLinkLog('$aLink[linkurl]') href='$aLink[linkurl]' target='_blank'>
				<img align='absmiddle' src='" . C ( 'web_root' ) . "Public/Linkicon/" . $aLink ['linkicon'] . "'>$aLink[linkname]</a>
				<div class='cenlink'><img title='收藏' src='" . C ( 'web_root' ) . $cenimgsrc . "' onclick=cenPublicLink('submit',$aLink[linkid],this)></div></div></td>";
			if ($num % 4 == 0)
				$table .= "</tr><tr>";
		}
		$table .= "</table>";
		
		return $table;
	}
	/* 网站二级分类获取 */
	function getType2Link($type) {
		$redis = getObj ( 'redis' );
		$linkInfo = $redis->getType2Link ( $type );
		
		$table = "<table id='table_link' border='0' cellpadding='10' cellspacing='0'>" . C ( "html_web_type1_" . substr ( $type, 0, 1 ) ) . "<tr>";
		$num = 0;
		foreach ( $linkInfo as $aLink ) {
			if ($aLink ['linkicon'] == 'publiceeb/guanggao.png')
				$aLink ['linkicon'] = 'publiceeb/guangg.jpg';
			
			if ($aLink ['linkicon'] == "")
				$aLink ['linkicon'] = "publiceeb/moren.png";
			
			$cenimgsrc = "Public/Image/cen1.png";
			if (in_array ( $aLink ['linkid'], session ( 'nwb_usercen' ) ))
				$cenimgsrc = "Public/Image/cen2.png";
			$num ++;
			$table .= "<td onmouseover=cenPublicLink('show','link_wrap_$aLink[linkid]') onmouseout=cenPublicLink('reset','link_wrap_$aLink[linkid]')><div class='link_wrap' id='link_wrap_$aLink[linkid]'>
			<a onclick=toLinkLog('$aLink[linkurl]') href='$aLink[linkurl]' target='_blank'>
			<img align='absmiddle' src='" . C ( 'web_root' ) . "Public/Linkicon/" . $aLink ['linkicon'] . "'>$aLink[linkname]</a>
			<div class='cenlink'><img title='收藏' src='" . C ( 'web_root' ) . $cenimgsrc . "' onclick=cenPublicLink('submit',$aLink[linkid],this)></div></div></td>";
			if ($num % 4 == 0)
				$table .= "</tr><tr>";
		}
		$table .= "</table>";
		
		return $table;
	}
	/* 公共链接获取 */
	function getPublicLink() {
		$redis = getObj ( 'redis' );
		$linkInfo = $redis->getPublicLink ();
		
		$table = "<table id='table_link' border='0' cellpadding='10' cellspacing='0'>";
		foreach ( $linkInfo as $linkhead => $links ) {
			if ($links [0] != null)
				$table .= "<tr><td>" . ucfirst ( $linkhead ) . "</td>";
			$num = 0;
			foreach ( $links as $aLink ) {
				if ($aLink ['linkicon'] == 'publiceeb/guanggao.png') {
					$aLink ['linkicon'] = 'publiceeb/guangg.jpg';
				}
				
				if ($aLink ['linkicon'] == "")
					$aLink ['linkicon'] = "publiceeb/moren.png";
				
				$cenimgsrc = "Public/Image/cen1.png";
				
				if (in_array ( $aLink ['linkid'], session ( 'nwb_usercen' ) ))
					$cenimgsrc = "Public/Image/cen2.png";
				
				if ($num % 4 == 0 && $num != 0)
					$table .= "</tr><tr><td></td>";
				
				$table .= "<td onmouseover=cenPublicLink('show','link_wrap_$aLink[linkid]') onmouseout=cenPublicLink('reset','link_wrap_$aLink[linkid]')>
						<div class='link_wrap' id='link_wrap_$aLink[linkid]'>
						<a onclick=toLinkLog('$aLink[linkurl]') href='$aLink[linkurl]' target='_blank'>
						<img align='absmiddle' src='" . C ( 'web_root' ) . "Public/Linkicon/" . $aLink ['linkicon'] . "'>$aLink[linkname]</a>
						<div class='cenlink'><img title='收藏' src='" . C ( 'web_root' ) . $cenimgsrc . "' onclick=cenPublicLink('submit',$aLink[linkid],this)></div></div></td>";
				$num ++;
			}
			if ($links [0] != null)
				$table .= "<tr><td colspan = '6'><div class='div_hr'></div></td></tr>";
		}
		$table .= "</tr>";
		$table .= "</table>";
		return $table;
	}
	
	/* 私有链接获取 */
	function getPrivateLink() {
		$redis = getObj ( 'redis' );
		$linkInfo = $redis->getPrivateLink ();
		
		/* 格式化连接输出 */
		$table = "<table id='table_link' border='0' cellpadding='10' cellspacing='0'><tr>";
		$num = 0;
		foreach ( $linkInfo as $aLink ) {
			if ($aLink ['linkicon'] == 'publiceeb/guanggao.png')
				$aLink ['linkicon'] = 'publiceeb/guangg.jpg';
			
			if ($aLink ['linkicon'] == "")
				$aLink ['linkicon'] = "publiceeb/moren.png";
			
			$num ++;
			$table .= "<td><div class='link_wrap'>
					<a onclick=toLinkLog('$aLink[linkurl]') href='$aLink[linkurl]' target='_blank'><img align='absmiddle' src='" . C ( 'web_root' ) . "Public/Linkicon/" . $aLink ['linkicon'] . "'>
					$aLink[linkname]</a>
					<div class='dellink'><img src='" . C ( 'web_root' ) . "Public/Image/dellink.png' onclick=delPrivateLink('submit',$aLink[linkid])></div></div></td>";
			if ($num % 4 == 0)
				$table .= "</tr><tr>";
		}
		$table .= "</tr></table>";
		
		return $table;
	}
	
	/* 私有链接添加 */
	function addPrivateLink() {
		if (isset ( $_POST ['add_link_name'] )) {
			$linkname = $_POST ['add_link_name'];
			$linkurl = $_REQUEST ['add_link_url'];
			$linkicon = $_FILES ['add_link_icon'];
			
			if (substr ( $linkurl, 0, 4 ) != "http") {
				$linkurl = "http://" . $linkurl;
			}
			
			if ($linkicon [tmp_name] != "") {
				$upload = new \Think\Upload ();
				$upload->rootPath = './Public/Linkicon/';
				$upload->savePath = '';
				
				$info = $upload->uploadOne ( $linkicon );
				if (! info) {
					$this->error ( $upload->getError () );
				} else {
					$linkicon = date ( "Y-m-d" ) . "/" . $info ['savename'];
				}
			} else {
				$linkicon = "";
			}
			
			$data = array (
					'linkname' => $linkname,
					'linkurl' => $linkurl,
					'linkicon' => $linkicon 
			);
			
			$redis = getObj ( 'redis' );
			$status = $redis->addPrivateLink ( $data );
			
			if ($status) {
				/* 日志生成 */
				$logContent = session ( "nwb_userid" ) . "," . session ( "nwb_email" ) . ",cenLink," . $linkurl;
				writeLog ( $logContent, 'userOps' );
				
				toAlert ( "添加成功" );
				$this->redirect ( "/Home/Index/inPrivateLink" );
			} else {
				$this->error ( "添加失败！请重试!" );
			}
		} else {
			$this->error ( "上传信息获取失败!" );
		}
	}
	
	/* 私有链接删除 */
	function delPrivateLink($linkid) {
		$data ['linkid'] = $linkid;
		
		$redis = getObj ( 'redis' );
		$status = $redis->delPrivateLink ( $data );
		/* 日志生成 */
		$logContent = session ( "nwb_userid" ) . "," . session ( "nwb_email" ) . ",delLink," . $linkid;
		writeLog ( $logContent, 'userOps' );
		
		return $linkid;
	}
	
	/* 公有链接收藏至私有 */
	function cenPublicLink($pubLinkId) {
		$data ['pubLinkId'] = $pubLinkId;
		
		$redis = getObj ( 'redis' );
		$status = $redis->cenPublicLink ( $data );
		
		if ($status) {
			/* 日志生成 */
			$logContent = session ( "nwb_userid" ) . "," . session ( "nwb_email" ) . ",cenLink," . $pubLinkId;
			writeLog ( $logContent, 'userOps' );
		}
		
		return $status;
	}
}